<?php

namespace app\admin\validate\curriculum;

use app\admin\validate\Validates;

class Curriculum extends Validates
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */	
	protected $rule = [
        'ids|标识' => 'require|array',
        'id|参数' => 'require|>:0',
	    'name|课程名称' => 'max:100',
        'cycle|课程周期' => 'date',
        'type|课程类型' => 'eq:1',
        'cover|封面' => 'require|checkReal|checkImage',
        'start_cycle|课程周期开始' => 'require|date',
        'end_cycle|课程周期结束' => 'require|date|dateEndGtEqStart:start_cycle',
        'origin_price|原价' => 'float',
        'price|价格' => 'require|float',
        'tid|授课教师' => 'require|array',
        'category|课程分类' => 'number'
    ];
    
    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */	
    protected $message = [];

    /**
     * 定义场景
     * @var []
     */
    protected $scene = [
        'index' => ['name','cycle','type'],
        'delete' => ['id'],
        'destory' => ['ids'],
    ];

    /**
     * 定义首页搜索场景验证
     * @return $this
     */
    public function sceneIndex()
    {
        $config = config('field.curriculum.category');
        return $this->only(['name','cycle','type','category'])
            ->append('category', 'in:'.implode(array_keys($config)));
    }

    /**
     * 定义新增场景验证
     * @return $this
     */
    public function sceneCreate()
    {
        $config = config('field.curriculum.category');
        return $this->only(['name','type','cover','start_cycle','end_cycle','origin_price','price','tid'])
            ->append('name', 'require')
            ->append('type', 'require')
            ->append('category', 'require|in:'.implode(array_keys($config)));
    }

    /**
     * 定义编辑场景验证
     * @return $this
     */
    public function sceneUpdate()
    {
        $config = config('field.curriculum.category');
        return $this->only(['name','type','cover','start_cycle','end_cycle','origin_price','price','tid'])
            ->append('name', 'require')
            ->append('type', 'require')
            ->append('category', 'require|in:'.implode(array_keys($config)));
    }

}
